package problems.cigarettesmokers;

import java.util.Random;
import problems.BenchmarkSemaphore;

public abstract class AbstractCigaretteSmokerSolution
{
    private BenchmarkSemaphore agentSemaphore;
    private BenchmarkSemaphore createdMatch;
    private BenchmarkSemaphore createdPaper;
    private BenchmarkSemaphore createdTobacco;

    protected AbstractCigaretteSmokerSolution()
    {
        agentSemaphore = new BenchmarkSemaphore(1);
        createdTobacco = new BenchmarkSemaphore(0);
        createdPaper = new BenchmarkSemaphore(0);
        createdMatch = new BenchmarkSemaphore(0);
    }

    public BenchmarkSemaphore getAgentSemaphore()
    {
        return agentSemaphore;
    }

    public BenchmarkSemaphore getCreatedMatch()
    {
        return createdMatch;
    }

    public BenchmarkSemaphore getCreatedPaper()
    {
        return createdPaper;
    }

    public BenchmarkSemaphore getCreatedTobacco()
    {
        return createdTobacco;
    }
    
    Random random = new Random();

    public void createTobaccoAndPaper()
    {
        agentSemaphore.waitOne();
        if(random.nextInt(1) == 0) {
        	createdPaper.release();
        	createdTobacco.release();
        }
        else {
	        createdTobacco.release();
	        createdPaper.release();
        }
    }

    public void createPaperAndMatch()
    {
        agentSemaphore.waitOne();
        if(random.nextInt(1) == 0) {
	        createdPaper.release();
	        createdMatch.release();
        }
        else {
        	createdMatch.release();
        	createdPaper.release();
        }
    }

    public void createTobaccoAndMatch()
    {
        agentSemaphore.waitOne();
        if(random.nextInt(1) == 0) {
        	createdMatch.release();
        	createdTobacco.release();
        }
        else {
	        createdTobacco.release();
	        createdMatch.release();
        }
    }

    public abstract void pushTobacco();
    public abstract void pushPaper();
    public abstract void pushMatch();
    public abstract void consumeResourcesAsSmokerWithMatch();
    public abstract void consumeResourcesAsSmokerWithTobacco();
    public abstract void consumeResourcesAsSmokerWithPaper();
}